#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>

static int sock=0;
static int acceptSock=0;
void func(int signal)
{
    close(sock);
    close(acceptSock);
    raise(SIGINT);
}

int main(int argc,char *argv[])
{
    signal(SIGTSTP,func);
    if(argc!=3)
    {
        printf("invalid number!\n");
        return -1;
    }
    sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock<0)
    {
        perror("socket:");
        return -1;
    }

    int isUse=1;
    setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&isUse,4);

    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    if(bind(sock,(struct sockaddr *)&addr,sizeof(addr))!=0)
    {
        perror("bind:");
        return -1;
    }
    if(listen(sock,10)!=0)
    {
        perror("listen:");
        return -1;
    }

    struct sockaddr_in clientAddr;
    socklen_t len;
    acceptSock=accept(sock,(struct sockaddr *)&clientAddr,&len);
    if(acceptSock<0)
    {
        perror("accept:");
        return -1;
    }
    while(1)
    {
        char str[100]={0};
        if(recv(acceptSock,str,100,0)<0)
        {
            perror("recv:");
            return -1;
        }
        printf("recv from client:%s\n",str);
    }
    return 0;
}